package com.ljkj.lawyer.lawyer.service;

import cn.hutool.core.io.FileUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.ljkj.lawyer.lawyer.chainmaker.AddHashCodeByUserIdParam;
import com.ljkj.lawyer.lawyer.common.MessageResult;
import com.ljkj.lawyer.lawyer.ipfs.AddResult;
import com.ljkj.lawyer.lawyer.ipfs.SelectResult;
import com.ljkj.lawyer.lawyer.param.HttpMemberRes;
import com.ljkj.lawyer.lawyer.param.HttpRes;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.Map;

@Service
public class HttpService {
    @Value("${ipfs.baseUrl}")
    private String baseUrl;
    @Value("${chainmaker.baseUrl}")
    private String chainmakerUrl;

    @Value("${member.baseUrl}")
    private String memberUrl;
    private final String getString = "";
    private final String addString = "addString";
    private final String uploadFile = "uploadFile";
    private final String downloadFile = "downloadFile/";
    private final String addHashCodeByUserId = "addHashCodeByUserId";
    private final String getHashCodeListByUserId = "getHashCodeListByUserId";


    public SelectResult getString(String hashCode) {
        String url = baseUrl.concat(hashCode);
        String result = this.get(url, "");
        try {
            return JSON.parseObject(result, SelectResult.class);
        }catch (Exception e){
            System.out.println("getStringError:" + e);
        }
        return null;

    }

    public AddResult addString(String dateString) {
        String url = baseUrl.concat(addString);
        Map<String, String> paramMap = new HashMap<>();
        paramMap.put("dateString", dateString);
        String request = JSON.toJSONString(paramMap);
        String result =  this.post(url, request);
        try {
            return JSON.parseObject(result, AddResult.class);
        }catch (Exception e){
            System.out.println("addStringError:" + e);
        }
        return null;
    }

    public MessageResult addHashCodeToChainmaker(AddHashCodeByUserIdParam addHashCodeByUserIdParam) {
        String url = chainmakerUrl.concat(addHashCodeByUserId);
        String request = JSON.toJSONString(addHashCodeByUserIdParam);
        String result =  this.post(url, request);
        try {
            return JSON.parseObject(result, MessageResult.class);
        }catch (Exception e){
            System.out.println("addHashCodeToChainmakerError:" + e);
        }
        return null;
    }

    public MessageResult getHashCodeFromChainmaker(Integer userId ,String contractName) {
        String url = chainmakerUrl.concat(getHashCodeListByUserId);
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("userId", userId);
        paramMap.put("contractName", contractName);
        String request = JSON.toJSONString(paramMap);
        String result =  this.get(url, request);
        try {
            return JSON.parseObject(result, MessageResult.class);
        }catch (Exception e){
            System.out.println("getHashCodeFromChainmakerError:" + e);
        }
        return null;
    }

    public HttpMemberRes getMember(Integer memberId) {
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("memberId", memberId);
        String request = JSON.toJSONString(paramMap);
        String result =  this.post(memberUrl, request);
        try {
            HttpRes httpRes = JSON.parseObject(result, HttpRes.class);
            return httpRes.getData();
        }catch (Exception e){
            System.out.println("getMemberError:" + e);
        }
        return null;
    }

    public AddResult uploadFile(MultipartFile file) {
        String url = baseUrl.concat(uploadFile);
        String result =  this.uploadFile(url, file);
        System.out.println(result);
        try {
            return JSON.parseObject(result, AddResult.class);
        }catch (Exception e){
            System.out.println("uploadFileError:" + e);
        }
        return null;
    }

    public byte[] downloadFile(String hashCode) {
        String url = baseUrl.concat(downloadFile);
        return  this.downloadFile(url,hashCode);
    }


    public String post(String url, String json) {
        String result = HttpRequest.post(url)
                .body(json)
                .execute().body();
        System.out.println(result);
        return result;
    }

    public String get(String url, String json) {
        String result = HttpRequest.get(url)
                .body(json)
                .execute().body();
        return result;

    }

    public String uploadFile(String url, MultipartFile file) {
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("file", file);
        String result = HttpUtil.createPost(url)
                .header("Content-Type","multipart/form-data;")
                .form(paramMap)
                .execute().body();
        return result;
    }

    public byte[]  downloadFile(String url, String hashCode) {
        url = url + hashCode;
        return HttpUtil.downloadBytes(url);
    }

    public static void main(String[] args) {
        String a = "{\"codeType\":2,\"hashCode\":\"QmXDgVdZ2Wm5zduLDqVE6FnZnCSfjpqidYFJ4C2XX5iydT\"}";
        System.out.println(a.substring(26,a.length()-2));
    }



}
